package com.gopay.scheduler.job;

import com.gopay.common.constants.SubAccountConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.acct.CommonQueryDAO;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.domain.cps.transfer.Trans00606VO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * 生成分账订单定时任务
 * 
 * @SubAccountInfoJob.java
 * @author zhuliang
 * @2017-7-25 下午3:39:35 www.gopay.com.cn Inc.All rights reserved.
 */
public class SubAccountInfoJob extends GopayBaseJob {
    // 日志
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "comQueryDao")
    private CommonQueryDAO commonQueryDAO;

    @Resource(name = "cpsGeneralMainOrderQueryDAO")
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderQueryDAO;

    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;

    @Override
    public void doJobInternal() {
        logger.info("开始查询待分账的网关交易记录!");
        //1.查询一天的待收款分账的记录
        List<Object[]> result = commonQueryDAO.findBySql(buildSql());
        if(result == null || result.size() == 0){
            logger.info("待收款分账结果集为null，收款分账定时结束任务。");
            return ;
        }
        logger.info("待分账的网关支付交易的记录数 = " + result.size());

        //2.处理结果集
        for(int i = 0; i<result.size();i++){
            // 调用CPS
            Object[] list = result.toArray();
            try {
                String gopayOrderId = list[i].toString();
                Trans00606VO vo = new Trans00606VO();
                //00100的订单号
                vo.setDtlSq(gopayOrderId);
                vo.setIsForceRecv(SubAccountConstants.SPLIT_Force_0);
                vo.setTranCode(IntTxnCd._00606.value);
                vo.setRemark("收款分账");
                logger.error("处理第【"+(i+1)+"】条记录，调用cps，网关支付交易流水号="+gopayOrderId);
                CPSResponse response = callCPSHelper.callByVOTicket(vo);
                if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
                    logger.error("------调用cps返回异常  response=" + response.getRespCode());
                }
                logger.info("第" + (i+1) + "条处理成功,网关支付交易流水号=" + gopayOrderId);
            }catch (GopayException e) {
                logger.error("收款分账定时任务Gopay异常", e);
            } catch (Exception e) {
                // 吃掉所有异常
                logger.error("收款分账定时任务异常", e);
            }
        }
        logger.error("收款分账定时任务结束");
    }

    private String buildSql(){
        String endDate = DateUtils.getYesterday();
        String sql = "select a.gopay_order_id from cps_gen_main_order a " +
                "left join cps_gen_receipt_order b on a.gopay_order_id=b.gopay_order_id" +
                " where 1=1  and a.txn_sta_cd = 20000  and b.other_recv_info is not null and b.OTHER_RECV_STATUS='1' and b.OTHER_RECV_SYTLE = '1' and a.gopay_int_txn_cd = '00100'"+
                "and a.gopay_txn_tm >= to_date('"+endDate+"','yyyy-MM-dd') ";
        return sql;

    }
}
